package com.agh.portal.domain;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import com.agh.portal.data.Votable;
import java.util.HashSet;
import javax.persistence.CascadeType;

@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findAnswersByStudent", "findAnswersByQuestion" })
public class Answer implements Votable {

    @Size(max = 1000)
    @NotEmpty
    private String content;

    private Integer goodVoteCount = 0;

    private Integer badVoteCount = 0;

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private Set<Student> proVoters = new HashSet<Student>();

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private Set<Student> conVoters = new HashSet<Student>();

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date creationDate;

    @NotNull
    @ManyToOne
    private Question question;

    @NotNull
    @ManyToOne
    private Student student;

    public String toString() {
        return "Answer [ content=" + content.substring(0, Math.min(30, content.length())) + "..." + " creationDate=" + creationDate + " goodVoteCount=" + goodVoteCount + " badVoteCount=" + badVoteCount + " question=" + question.getTitle() + " student=" + student.getName() + "." + student.getSurname() + "]";
    }

    public static List<Answer> findAllAnswers() {
        return entityManager().createQuery("SELECT o FROM Answer o ORDER BY o.creationDate desc", Answer.class).getResultList();
    }

    public static List<Answer> findAnswerEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Answer o ORDER BY o.creationDate desc", Answer.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    public static TypedQuery<Answer> findLatestAnswersByStudent(Student student, Integer maxResults) {
        if (student == null) throw new IllegalArgumentException("The student argument is required");
        EntityManager em = Answer.entityManager();
        TypedQuery<Answer> q = em.createQuery("SELECT o FROM Answer AS o WHERE o.student = :student ORDER BY o.creationDate desc", Answer.class);
        q.setParameter("student", student);
        if (maxResults != null) {
            q.setMaxResults(maxResults);
        }
        return q;
    }

    public static TypedQuery<Answer> findLatestAnswers(Integer maxResults) {
        EntityManager em = Answer.entityManager();
        TypedQuery<Answer> q = em.createQuery("SELECT o FROM Answer AS o ORDER BY o.creationDate desc", Answer.class);
        if (maxResults != null) {
            q.setMaxResults(maxResults);
        }
        return q;
    }

    public void addGoodVote() {
        this.goodVoteCount++;
    }

    public void decrementGoodVote() {
        this.goodVoteCount--;
    }

    public void addBadVote() {
        this.badVoteCount++;
    }

    public void decrementBadVote() {
        this.badVoteCount--;
    }

}
